{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# PMOD ALS Sensor demonstration\n",
    "\n",
    "This demonstration shows how to use the PmodALS. You will also see how to plot a graph using matplotlib.\n",
    "\n",
    "The PmodALS and a light source is required. E.g. cell phone flashlight.\n",
    "\n",
    "The ambient light sensor is initialized and set to log a reading every 1 second.  The sensor can be covered to reduce the light reading, and a light source can be used to increase the light reading"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. Use ALS read() to read the current room light"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from pynq.overlays.base import BaseOverlay\n",
    "base = BaseOverlay(\"base.bit\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "42"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pynq.lib import Pmod_ALS\n",
    "\n",
    "# ALS sensor is on PMODB\n",
    "my_als = Pmod_ALS(base.PMODB)\n",
    "my_als.read()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. Starting logging light once every second"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "my_als.start_log()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. Modifying the light\n",
    "\n",
    "* Decrease the light reading by covering the sensor\n",
    "* Increase the light by shining a flashlight on the device\n",
    "\n",
    "Stop the logging whenever you are finished trying to change the sensor's value."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "my_als.stop_log()\n",
    "log = my_als.get_log()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4. Plot values over time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAEKCAYAAAACS67iAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAFWBJREFUeJzt3X+QJGd93/H3dy2DiyORhYIkdk+cNusfiF9RSCGbkmPm\nUiUBdllHXLGC2bM4Y1ykCGeXjapAJKPdY1MOxGeqHGxVpYysE7ojQgKDIE6QFuvWlBLOsg0CGcny\ncbd3oF10EkjguiNxSew3f0zPMjrtj9md3e3ZZ9+vqq6b6+mefqZr9jPd337m6chMJEmb30DdDZAk\nrQ0DXZIKYaBLUiEMdEkqhIEuSYUw0CWpEAa6VLOIuDki3ld3O7T5GejaUBExFRFPRMQPnzV/0VCL\niF0R8aWI+E5EPBYRn4uIHYssOxQRH4+IxyPiyYj4SkRcux7vReo3Bro2TBXClwOPAVd3uc4IcAvw\nW5n5o8Aw8IfA9xdZ5VbgJHAxcD7wK8Cp3lq+diLih+pug8ploGsjXQtMAh8B9nS5zmXA8cycAsjM\nM5n5ycx8ZJHlXw3ckpn/LzPnMvPLmXlX+8mI+OmI+N/V0fuXIuK1Hc8djoj3RcS9EfH3EfHZiHhB\n9dxzI+LWiPhWte5fRMQLq+deFBF3RsS3I+LvIuJtHa85FhF3VOt+B3jLcm84In49Io5W2/pURLyo\n47mrIuJvqzb8YXXG89Yu96UKZ6BrI10LfAy4A3hdOxCX8UXgJRHxwYhoRMS2ZZb/AnBjRPzbiLi4\n84mIGAT+B/C+zDwPuA74RESc37HYL9MK3RcCz62WoZr3j4Eh4AXAvwP+b/Xcx4CvAxcBvwT8TkQ0\nOl7zauD26gzj0FKNj4h/BfwO8G+AF1Wve1v13D+hte/eTevs42HgNcvsD20hBro2RET8DK0w/HRm\nHgW+Crx5ufUycxpoAIO0gvPxqt7+vEVW+SXg88B/BI5XR+H/onpuN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cblz8zv1NXGfrLI/tpJqzY8\nB5wA3t6uD291EXEF8HlaN/DJanovcB9wO330GfOn/5JUCC+KSlIhDHRJKoSBLkmFMNAlqRAGuiQV\nwkCXpEIY6JJUCANdkgrx/wFHMGUS8gfoqgAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x317a1e90>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "plt.plot(range(len(log)), log, 'ro')\n",
    "plt.title('ALS Sensor log')\n",
    "plt.axis([0, len(log), min(log), max(log)])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.4.3+"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
